#include "StdAfx.h"
#include "BaseCtrl.h"
#include <windowsx.h>

CBaseCtrl::CBaseCtrl(void)
{
	m_hWndOwner		=	NULL;
	m_uID			=	0;
	memset(&m_rtArea,0,sizeof(RECT));

	//m_ctType		=	CT_UNKNOWN;
	m_bShow			=	TRUE;
}

CBaseCtrl::~CBaseCtrl(void)
{
}

BOOL CBaseCtrl::DoWndProc( UINT message , WPARAM wParam ,LPARAM lParam ,LRESULT* pResult)
{
	BOOL bRet = FALSE;
	switch(message) 
	{
	case WM_LBUTTONDOWN:
		{
			POINT ptMouse = {GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)};
			bRet = HitTest(ptMouse);
			if(bRet)
			{
				DoLButtonDown(ptMouse);
				*pResult = BCRET_MSG_CATCH;
			}
		}
		break;
	case WM_LBUTTONUP:
		{
			POINT ptMouse = {GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)};
			bRet = HitTest(ptMouse);
			if(bRet)
			{
				DoLButtonUp(ptMouse);
				*pResult = BCRET_MSG_CATCH;
			}
		}
		break;
	case WM_MOUSEMOVE:
		{
			POINT ptMouse = {GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)};
			bRet = DoMouseMove(ptMouse);
			//if(bRet)
			//{
			//	*pResult = BCRET_MSG_CATCH;
			//}
			bRet = FALSE;
		}
		break;
	case WM_MOUSELEAVE:
		{
			DoMouseLeave();
		}
		break;
	case WM_RBUTTONDOWN:
		break;
	case WM_RBUTTONUP:
		break;
	case WM_KEYDOWN:
		OnKeyDown(wParam,lParam,pResult);
		break;
	case WM_DESTROY:
		break;
	case WM_COMMAND:
		OnCommand(wParam,lParam);
		break;
	default:
		break;
	}

	return bRet;
}

void CBaseCtrl::Draw( HDC hdc )
{
	if(!IsVisible())
		return;


	OnDraw(hdc);
}


void CBaseCtrl::OnDraw( HDC hdc )
{
}

BOOL CBaseCtrl::DoLButtonDown( POINT point )
{
	return FALSE;
}

BOOL CBaseCtrl::DoLButtonUp( POINT point )
{
	return FALSE;
}

BOOL CBaseCtrl::DoMouseMove( POINT point )
{
	BOOL bInCtrl = FALSE;
	
	if(HitTest(point))
	{
		bInCtrl = TRUE;
		DoMouseOver();
	}
	else
	{
		DoMouseLeave();
	}

	return bInCtrl;
}

void CBaseCtrl::DoMouseLeave()
{
}

void CBaseCtrl::DoMouseOver()
{
}

void CBaseCtrl::MoveCtrlEx( POINT ptLeftTop,BOOL bRepaint )
{
	RECT rtNew = {0};
	rtNew.right  = ptLeftTop.x + m_rtArea.right - m_rtArea.left;
	rtNew.bottom = ptLeftTop.y + m_rtArea.bottom - m_rtArea.top;
	rtNew.left	= ptLeftTop.x;
	rtNew.top	= ptLeftTop.y;

	MoveCtrl(&rtNew,bRepaint);
}

void CBaseCtrl::MoveCtrl( LPRECT lprtArea,BOOL bRepaint )
{
	SetCtrlRect(lprtArea);

	if(bRepaint)
	{
		InvalidateWnd(FALSE);
	}
}
void CBaseCtrl::SetCtrlRect( LPRECT lprtArea )
{
	if(NULL == lprtArea)
		return;
	CopyRect(&m_rtArea,lprtArea);
}
void CBaseCtrl::GetCtrlRect( LPRECT lprtArea )
{
	if(NULL == lprtArea)
		return;
	CopyRect(lprtArea,&m_rtArea);
}

BOOL CBaseCtrl::HitTest( POINT point )
{
	BOOL bInCtrl = FALSE;

	if(!IsVisible())
		return FALSE;

	if(PtInRect(&m_rtArea,point))
	{
		bInCtrl = TRUE;
	}

	return bInCtrl;
}

void CBaseCtrl::Show( BOOL bShow /*= TRUE*/ )
{
	m_bShow = bShow;
	InvalidateWnd(FALSE);
}

BOOL CBaseCtrl::IsVisible()
{
	return m_bShow;
}


BOOL CBaseCtrl::OnKeyDown( WPARAM wParam ,LPARAM lParam ,LRESULT* pResult )
{
	return FALSE;
}

void CBaseCtrl::OnCommand( WPARAM wParam ,LPARAM lParam )
{
}

void CBaseCtrl::InvalidateWnd( BOOL bErase )
{
	if(IsWindow(m_hWndOwner))
		InvalidateRect(m_hWndOwner,&m_rtArea,bErase);
}
